package com.showcai.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.AdminUserMapper;
import com.showcai.module.AdminUser;
import com.showcai.module.BaseModule;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.AdminUserService;
import com.showcai.service.ServiceBase;
import com.showcai.util.MD5Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AdminUserServiceImpl extends ServiceBase implements AdminUserService {

    @Autowired
    AdminUserMapper adminUserMapper;

    private void updateUser(AdminUser adminUser){
        adminUserMapper.updateAdminUserById(adminUser);
    };
    private void insertUser(AdminUser adminuser) {

        adminUserMapper.insertAdminUser(adminuser);

    }

    @Override
    public AdminUser login(AdminUser adminUser) {
        Map map = new HashMap();
        map.put("userName", adminUser.getUserName());
        map.put("password", MD5Tools.MD5(adminUser.getPassWord()));
        adminUser = adminUserMapper.selectAdminUser(map);
        if (adminUser == null) {
            throw new MyRunTimeExcption("账号或者密码错误！");
        }
        return adminUser;
    }
    @Override
    public PageInfo getUserList(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<AdminUser> list = adminUserMapper.selectAdminUserList();
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public AdminUser selectAdminUserName(String userName) {
        AdminUser adminUser = adminUserMapper.selectAdminUserName(userName);
        return adminUser;
    }


    @Override
    public void modifyAdminUser(AdminUser adminUser) {
        if(adminUser.getTid()==null){
            adminUser.setPassWord(MD5Tools.MD5("123456"));
            adminUser.setCreateTime(new Date());
            insertUser(adminUser);
            return;
        }
        System.out.println("停用的用户ID————————————"+adminUser.getTid());
        System.out.println("停用的用户————————————"+adminUser.getStatus());
        updateUser(adminUser);
    }

    @Override
    public void stopAdminUser(Long tid) {
        Map<String,String> map=new HashMap<>();
        map.put("tid",String.valueOf(tid));
        AdminUser adminUser = selectAdminById(map);
        if(adminUser==null){
            throw new MyRunTimeExcption("用户不存在！");
        }
        adminUser.setStatus(BaseModule.STATUS_UNVALID);
        this.modifyAdminUser(adminUser);
    }

    @Override
    public void operateAdminUser(Long tid) {
        AdminUser adminUser = new AdminUser();
        adminUser.setTid(tid);
        adminUser.setStatus(BaseModule.STATUS_VALID);
        this.modifyAdminUser(adminUser);
    }

    @Override
    public void resetPassword(Long tid) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("tid",String.valueOf(tid));
        map.put("passWord",MD5Tools.MD5("123456"));
        adminUserMapper.resetPassword(map);
    }

    @Override
    public AdminUser selectAdminById(Map<String, String> map) {

        return adminUserMapper.selectAdminById(map);
    }



}
